package com.benshou.bcss.common.taskscheduler.config;

import com.benshou.bcss.common.mdc.MDCTaskDecorator;
import com.benshou.bcss.common.taskscheduler.config.props.TaskSchedulerThreadPoolProps;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * Task Scheduler 配置
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Configuration
@EnableScheduling
public class TaskSchedulerConfig {

    /**
     * @param taskSchedulerThreadPoolProps Task Scheduler 线程池属性
     * @return Task Scheduler 线程池 Bean
     */
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler(
            TaskSchedulerThreadPoolProps taskSchedulerThreadPoolProps) {
        var scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(taskSchedulerThreadPoolProps.getPoolSize());
        scheduler.setThreadNamePrefix(taskSchedulerThreadPoolProps.getThreadNamePrefix());
        // MDC
        scheduler.setTaskDecorator(new MDCTaskDecorator());
        return scheduler;
    }
}
